package com.yaozheng.vocationaltraining.service.impl.collect;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IBaseView;
import com.yaozheng.vocationaltraining.iview.collect.IMyCollectDeleteView;
import com.yaozheng.vocationaltraining.service.collect.MyCollectDeleteService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

import java.util.HashMap;

/**
 * 删除收藏
 */
@EBean
public class MyCollectDeleteServiceImpl implements MyCollectDeleteService {
    IMyCollectDeleteView iMyCollectDeleteView;

    @Override
    public void init(IMyCollectDeleteView iMyCollectDeleteView) {
        this.iMyCollectDeleteView = iMyCollectDeleteView;
    }

    @Background
    @Override
    public void deleteCollect(int id) {
        try {
            HashMap<String, String> para = new HashMap<String, String>();
            para.put("id", String.valueOf(id));
            OkHttpClientManager.getInstance().post(
                    Constants.ROOTURL + "/user/delFavorites/" + id,
                    iMyCollectDeleteView.getToken(), para,
                    new OkHttpClientManager.IBaseViewCallback(new BaseView(iMyCollectDeleteView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iMyCollectDeleteView.deleteCollectSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iMyCollectDeleteView.deleteCollectError(error);
                        }


                    }));
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iMyCollectDeleteView.isResponseResult()) {
                iMyCollectDeleteView
                        .deleteCollectError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }
}
